/* Classic Ladder Project */
/* Copyright (C) 2001-2011 Marc Le Douarain */
/* http://www.multimania.com/mavati/classicladder */
/* http://www.sourceforge.net/projects/classicladder */
/* July 2009 */
/* ------- */
/* System vars */
/* ------- */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */

/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU */
/* Lesser General Public License for more details. */

/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef MODULE
#include <stdio.h>
#include <string.h>
#include <time.h>
#endif

#include "classicladder.h"
#include "global.h"
#include "vars_access.h"
#include "vars_system.h"

int FlashSecs;
int FlashMins;
int ColdStart;
int HotStart;

void InitSystemVars( char HardwareStart )
{
	FlashSecs = 0;
	FlashMins = 0;
	if ( HardwareStart )
		ColdStart = 1;
	HotStart = 1;
}

void UpdateSystemVars( void )
{
	time_t CurrentTime;
	struct tm * ptm;
	FlashSecs = FlashSecs+InfosGene->GeneralParams.PeriodMilliSecsTaskLogic;
	if ( FlashSecs>=1000 )
		FlashSecs = FlashSecs-1000;
	//1hz
	WriteVar( VAR_SYSTEM, 0, FlashSecs<500 );
	//~1Hz
	WriteVar( VAR_SYSTEM, 1, FlashSecs>=500 );

	FlashMins = FlashMins+InfosGene->GeneralParams.PeriodMilliSecsTaskLogic;
	if ( FlashMins>=60000 )
		FlashMins = FlashMins-60000;
	//1Min
	WriteVar( VAR_SYSTEM, 2, FlashSecs<30000 );
	//~1Min
	WriteVar( VAR_SYSTEM, 3, FlashSecs>=30000 );

	//boots
	WriteVar( VAR_SYSTEM, 4, ColdStart );
	WriteVar( VAR_SYSTEM, 5, HotStart );
	ColdStart = 0;
	HotStart = 0;

//TODO: see if it does not cause context switches...
#ifndef __XENO__
	CurrentTime = time(NULL);
	if ( CurrentTime!=(time_t)-1 )
	{
		ptm = localtime( &CurrentTime );
		WriteVar( VAR_WORD_SYSTEM, 0, ptm->tm_hour*10000+ptm->tm_min*100+ptm->tm_sec );
		WriteVar( VAR_WORD_SYSTEM, 1, (ptm->tm_year)%100*10000+(ptm->tm_mon+1)*100+ptm->tm_mday );
		WriteVar( VAR_WORD_SYSTEM, 2, ptm->tm_wday );
	}
#endif

//debug
WriteVar( VAR_WORD_SYSTEM, 9, FlashSecs );
}

